package com.cuit.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuit.mapper.Book.BookUserMapper;
import com.cuit.mapper.BookMapper;
import com.cuit.pojo.Book;
import com.cuit.pojo.BookUser;
import com.cuit.pojo.Book_type;
import com.cuit.result.R;
import com.cuit.service.BookService;
import com.cuit.service.BooktypeService;
import com.cuit.service.FileService;
import com.cuit.service.impl.ArticleRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * @author chishuiLi     Email:1192537447@qq.com
 * @Description
 */
@CrossOrigin //开放前端的跨域访问
@Api(tags = "书本管理")
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookUserMapper bookUserMapper;
    @Resource
    private ElasticsearchTemplate template;//es的复杂操作
    @Resource
    private FileService fileService;

    @Resource
    private ArticleRepository repository; //内置对文档的crud
    @Resource
    private BookService bookService;
    @Resource
    private BookMapper bookMapper;
    @Resource
    private BooktypeService booktypeService;



    @GetMapping("/list")
    @ApiOperation("获得所有图书测试接口")
    public R getAllBook(){
        List<Book> list = bookService.list();
        list.forEach(book -> {
            book.setBookimg(fileService.allPath(book.getBookimg()));
        });
        return R.ok().data("bookList",list);
    }

    @GetMapping("/list/{id}")
    @ApiOperation("根据id搜索图书")
    public R getBookById(@PathVariable Integer id){
        Book book = bookService.getById(id);
        List<Book> list = new ArrayList<>();
        if(book != null) {
            book.setBookimg(fileService.allPath(book.getBookimg()));
            list.add(book);
            return R.ok().data("bookList",list);
        }else {
            return R.error();
        }
    }
    @GetMapping("/searchAll")
    @ApiOperation("模糊查询")
    public R searchAll(HttpServletRequest request){
        String Name=request.getParameter("Name");
        String type=request.getParameter("type");
        boolean b = Objects.equals(type, "0") || Objects.equals(type, "1") || type.equals("0") || type.equals("1") || type.isEmpty();
        boolean b1 = Objects.equals(type, "1") || type.equals("1");
        if (Name==null||Name.isEmpty()||Name.equals("")){
            QueryWrapper<BookUser> queryWrapper=new QueryWrapper<>();
            if (b){
                queryWrapper=null;
            }else {
                queryWrapper.eq("bookTypeName",type);
            }
            List<BookUser> list=bookUserMapper.selectList(queryWrapper);
            if (b1){
                list.sort(Comparator.comparingLong(BookUser::getSellnum).reversed());
            }
            list.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
                e.setUserImg(fileService.allPath(e.getUserImg()));
            });
            return R.ok().data("bookList",list);
        }else {
            template.putMapping(BookUser.class);
            if (b){
                List<BookUser> list=repository.findBybookName(Name);
                List<BookUser> newList = new ArrayList(list);
                if (b1){
                    newList.sort(Comparator.comparingLong(BookUser::getSellnum).reversed());
                }
                newList.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
                    e.setUserImg(fileService.allPath(e.getUserImg()));
                });
                return R.ok().data("bookList",newList);
            }else {List<BookUser> list=repository.findByBookNameAndBookType(Name,type);
                List<BookUser> newList = new ArrayList(list);
                newList.sort(Comparator.comparingLong(BookUser::getSellnum).reversed());
                newList.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
                    e.setUserImg(fileService.allPath(e.getUserImg()));
                });
                return R.ok().data("bookList",newList);}
        }
    }
    @GetMapping("/hotAll")
    public R hotAll(){
        List<BookUser> list=bookUserMapper.selectList(null);
        list.sort(Comparator.comparingLong(BookUser::getSellnum).reversed());
        list.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
            e.setUserImg(fileService.allPath(e.getUserImg()));
        });
        return R.ok().data("bookList",list);
    }
    @GetMapping("/bytype")
    public R bytype(HttpServletRequest request){
        String type=request.getParameter("type");
        QueryWrapper<BookUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("booktypename",type);
        List<BookUser> list=bookUserMapper.selectList(queryWrapper);
        list.sort(Comparator.comparingLong(BookUser::getSellnum).reversed());
        list.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
            e.setUserImg(fileService.allPath(e.getUserImg()));
        });
        return R.ok().data("bookList",list);
    }
    @GetMapping("/All")
    public R All(){
        List<BookUser> list=bookUserMapper.selectList(null);
        list.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
            e.setUserImg(fileService.allPath(e.getUserImg()));
        });
        return R.ok().data("bookList",list);
    }
    @GetMapping("/bookType")
    @ApiOperation("书籍类型")
    public R bookType(){
        List<Book_type> list = booktypeService.list();
        System.out.println(list);
        return R.ok().data("bookList",list);
    }
    @GetMapping("/searchbytype")
    @ApiOperation("模糊查询")
    public R searchbytype(HttpServletRequest request){
        String Name=request.getParameter("Name");
        String type=request.getParameter("type");
        if (Name==null||Name.isEmpty()||Name.equals("")){
            QueryWrapper<BookUser> queryWrapper=new QueryWrapper<>();
            if (!Objects.equals(type, "猜你喜欢") &&!type.equals("猜你喜欢")){
                queryWrapper.eq("bookTypeName",type);
            }
            List<BookUser> list=bookUserMapper.selectList(queryWrapper);
            list.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
                e.setUserImg(fileService.allPath(e.getUserImg()));
            });
            return R.ok().data("bookList",list);
        }else {
            template.putMapping(BookUser.class);
            if (type=="猜你喜欢"||type.equals("猜你喜欢")||type.equals("")||type.isEmpty()){
                List<BookUser> list=repository.findBybookName(Name);
                list.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
                    e.setUserImg(fileService.allPath(e.getUserImg()));
                });
                return R.ok().data("bookList",list);
            }else {List<BookUser> list=repository.findByBookNameAndBookType(Name,type);
                list.forEach(e->{e.setBookImg(fileService.allPath(e.getBookImg()));
                    e.setUserImg(fileService.allPath(e.getUserImg()));
                });
                return R.ok().data("bookList",list);}
        }
    }
}

